package com.pet.server.ext.action.product;

import java.util.List;

import com.pet.server.ext.ActionBase;
import com.pet.server.ext.ApiError;
import com.pet.server.ext.Result;
import com.pet.server.ext.data.Constant;
import com.pet.server.ext.data.ConstantUi;
import com.pet.server.ext.util.Parameters;
import com.pet.server.ext.util.cache.ConstantUiUtil;
import com.pet.server.ext.util.cache.ConstantUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class Codelist extends ActionBase{

	private Integer product_id;
	public Codelist(){
		 needCheckAuth =false;
	}
	
	@Override
	public void initParameters(Parameters params) throws ApiError {	
		
	}
	
	@Override
	public Result invokeService() throws ApiError {
		JSONObject data = new JSONObject();
		Result result = new Result();
		ConstantUi ui = ConstantUiUtil.getInstance().findConstant("001");
		
		Constant mao = ConstantUtil.getInstance().findConstant("003001");
		Constant gou = ConstantUtil.getInstance().findConstant("003002");
		Constant shipin = ConstantUtil.getInstance().findConstant("001001");
		Constant yongpin = ConstantUtil.getInstance().findConstant("001002");
		
		ConstantUiUtil.init=false;
		ConstantUtil.init=false;
		
		
		data.put("mao", parseJson(mao));
		data.put("gou", parseJson(gou));
		data.put("shipin", parseJson(shipin));
		data.put("yongpin", parseJson(yongpin));
		data.put("jucode", parseJson(ui));
		
		
		
		result.put("data",data);
		return result;
	}
	
	public JSONObject parseJson(Constant cons){
		if(null==cons){
			return null;
		}
		JSONObject o = new JSONObject();
		o.put("cate_name",cons.conts_name);
		o.put("cate_code", cons.code);
		o.put("sub_category", toArraya(cons.list));
		return o;
	}
	
	
	public JSONObject parseJson(ConstantUi cons){
		if(null==cons){
			return null;
		}
		JSONObject o = new JSONObject();
		o.put("cate_name",cons.ui_name);
		o.put("cate_code", cons.code);
		o.put("sub_category", toArray(cons.list));
		return o;
	}
	
	public JSONArray toArraya(List<Constant> list){
		JSONArray array = new JSONArray();
		for(Constant cons:list){
			JSONObject o = new JSONObject();
			o.put("cate_name",cons.conts_name);
			o.put("cate_code", cons.code);
			
			o.put("sub_category", toArraya(cons.list));
			array.add(o);
		}
		return array;
	}
	
	
	//img_path,color_value,pet_breeds,pet_cate,pet_import
	public JSONArray toArray(List<ConstantUi> list){
		JSONArray array = new JSONArray();
		for(ConstantUi cons:list){
			JSONObject o = new JSONObject();
			o.put("cate_name",cons.ui_name);
			o.put("cate_code", cons.code);
			o.put("ico_path", null);
			o.put("img_path", cons.img_path);
			o.put("color_value",cons.color_value);
			o.put("sub_category", toArray(cons.list));
			array.add(o);
		}
		return array;
	}
	
	
	
	
}
